package xml;

import java.io.*;
import javax.xml.bind.*;

import problemSpace.*;


public class Marshalling {
	public static void marshall(ConcretePlanning map){
		marshall(map,"output.xml");
	}

	/**
	 * Convert a ConcretePlanning object to xml file
	 * @param map
	 * @param filename
	 */
	public static void marshall(ConcretePlanning map, String filename){
		try {
			JAXBContext context = JAXBContext.newInstance(ConcretePlanning.class);
			Marshaller m = context.createMarshaller();
			m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			//m.marshal(map, System.out);

			Writer w = null;
			try {
				w = new FileWriter(filename);
				m.marshal(map, w);
			} finally {
				try {
					w.close();
				} catch (Exception e) {
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * Convert a given xml file to object
	 * @param filename
	 * @return A ConcretePlanning object
	 */
	public static ConcretePlanning unmarshal(String filename){
		try {
			JAXBContext context = JAXBContext.newInstance(ConcretePlanning.class);
			Unmarshaller um = context.createUnmarshaller();
			ConcretePlanning map = (ConcretePlanning) um.unmarshal(new FileReader(filename));
			return map;
		} catch (JAXBException e) {
			System.out.println("ERROR: INLEZEN VAN DE XML");
			e.printStackTrace();
			
		} catch (FileNotFoundException e) {
			System.out.println("ERROR: FILE NOT FOUND");
		}
		return new ConcretePlanning();
	}

}

